//
//  THChoiceController.swift
//  TongHuoDemo
//
//  Created by rimi on 2017/5/4.
//  Copyright © 2017年 rimi. All rights reserved.
//

import UIKit

class THChoiceController: UITableViewController {

    var isRefresh: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupHeaderView()
        // 注册cell
        tableView.register(UINib.init(nibName: "THChoiceCell", bundle: nil), forCellReuseIdentifier: "choiceCell")
        tableView.register(UINib.init(nibName: "THChoiceFirstCell", bundle: nil), forCellReuseIdentifier: "firstCell")
        
        // 设置表格cell自适应高度
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        
        // 下拉刷新
        let refresh = UIRefreshControl()
        refresh.addTarget(self, action: #selector(refreshTableView), for: .valueChanged)
        tableView.refreshControl = refresh
    }
    
    // 下拉刷新的回调方法
    func refreshTableView() {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) { 
            self.isRefresh = true
            self.tableView.reloadData()
            self.tableView.refreshControl?.endRefreshing()
        }
    }
    
    // MARK: - UI相关
    func setupHeaderView() {
        let imagesArray = ["好店图片","人物图片"]
        // 创建一个V来设置头部视图高度
        let v = UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 298))
        
        // 三方框架添加滚动视图
        let scrollV = FFBannerView(frame: CGRect(x: 0, y: 0, width: screenW, height: 188), locationImageArray: imagesArray)
        scrollV?.timeInterval = 5.0
        scrollV?.pageControlStyle = FFPageControlStyleMiddle
        scrollV?.pageControl.dotSize = CGSize(width: 16, height: 4)
        scrollV?.pageControl.currentDotColor = UIColor.orange
        scrollV?.pageControl.dotColor = UIColor.lightGray
        v.addSubview(scrollV!)
        
        // 添加头部按钮视图
        let headerV = THChoiceHeaderView.choiceView()
        headerV.frame = CGRect(x: 0, y: 188, width: screenW, height: 110)
        v.addSubview(headerV)
        // 将设置好的头部视图赋值给表格视图
        tableView.tableHeaderView = v
        
        // 获取按钮的点击回调
        headerV.choiceHeaderViewBtnCallback = {(clickedBtn) -> () in
            print(clickedBtn.currentTitle ?? "")
        }
    }

    // MARK: - 表格视图数据源方法
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return isRefresh == false ? 1 : 0
        }
        
        return 10
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath) as! THChoiceFirstCell
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "choiceCell", for: indexPath) as! THChoiceCell
        cell.selectionStyle = .none
        // 传递数据

        return cell
    }


}
